package com.qf.mvc01.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {

    @RequestMapping("/test1")
    public Map<String,String> test1(HttpServletRequest req, HttpServletResponse resp) throws InterruptedException, IOException {
        //判断当前用户是不是登录过 ，约定登陆过的请求头中都会带有一个特殊的字段token
        String token = req.getHeader("token");
        HashMap<String,String> map = new HashMap<>();

        if (token != null){
            map.put("code","1");
            map.put("msg","ok");
        }else {
            map.put("code","-1");
            map.put("msg","当前用户没有登入");
        }
        Thread.sleep(1000);
        return map;
    }

    @RequestMapping("/del")
    public Map<String,String> delProduct(HttpServletRequest request){
        // 判断当前用户是不是登录过 ，约定登陆过的请求头中都会带有一个特殊的字段token
        final String token = request.getHeader("token");

        final HashMap<String, String> map = new HashMap<>();

        if (token!=null){  // 如果token不为空，代表用户登录过
            map.put("code","1");
            map.put("msg","ok");

        }else {
            map.put("code","-1");
            map.put("msg","当前用户没有登录");
        }

        return map;

    }
}
